﻿// -----------------------------------------------------------------------
// <copyright file="UnixDateTime.cs" company="none">
// Copyright (c) 2012 M. Alberti, xanatos(at)live.it
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// </copyright>
// -----------------------------------------------------------------------

namespace FluentSerializer
{
    using System;

    /// <summary>
    /// 
    /// </summary>
    public static class UnixDateTime
    {
        /// <summary>
        /// 
        /// </summary>
        public static readonly DateTime BaseUnixDateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(long time)
        {
            return BaseUnixDateTime.AddTicks(time * TimeSpan.TicksPerSecond);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ToUnixTime(DateTime dt)
        {
            return dt.Subtract(BaseUnixDateTime).Ticks / TimeSpan.TicksPerSecond;
        }
    }
}